package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.request.RefreshTokenRequest;
import net.xdclass.request.UserLoginRequest;
import net.xdclass.request.UserRegisterRequest;
import net.xdclass.service.FileService;
import net.xdclass.service.UserService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2022/4/4 18:01
 * @Created by sunhao
 */
@Api(tags = "用户信息模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

  @Autowired
  private FileService fileService;

  @Autowired
  private UserService userService;

  /**
   * 上传文件
   */
  @ApiOperation(value = "用户头像上传文件")
  @PostMapping("/upload")
  public JsonData uploadFile(@ApiParam(value = "文件上传",required = true)
                             @RequestPart("file") MultipartFile file){
    return JsonData.buildSuccess(fileService.uploadUserHeadImg(file));
  }


  /**
   * 用户注册
   * @return
   */
  @PostMapping("/register")
  @ApiOperation(value = "用户注册")
  public JsonData UserRegister(@ApiParam(value = "用户注册对象")@RequestBody UserRegisterRequest userRegisterRequest){
    return userService.userRegister(userRegisterRequest);
  }


  /**
   * 登录
   */
  @PostMapping("/login")
  @ApiOperation(value = "用户登录")
  public JsonData userLogin(@ApiParam(value = "用户登录对象")@RequestBody UserLoginRequest userLoginRequest){
    return  userService.userLogin(userLoginRequest);
  }


  /**
   * 刷新accessToken
   */
  @PostMapping("/refresh_token")
  @ApiOperation(value = "刷新accessToken")
  public JsonData refreshToken(@ApiParam(value = "刷新accessToken对象")@RequestBody RefreshTokenRequest refreshTokenRequest){
    return  userService.refreshToken(refreshTokenRequest);
  }


  /**
   * 查询用户详情
   */
  @GetMapping("/detail")
  @ApiOperation(value = "用户个人信息查询")
  public JsonData detail(){
     UserVo userVo = userService.findUserDetail();
     return JsonData.buildSuccess(userVo);
  }



}
